import { addOrder, editOrder } from '../../services/order'
import { checkIfLogin } from '../../utils/check'
const app = getApp()

Page({
  data: {
    selectedInfo:{},
    vehicleInfo:{},
    projectInfo:{},
    appointmentTimeInfo:{},

    appointTimeInfo:"",
    showDialogAlert: false,
    dialogMsg:"",
  },

  onLoad(){
    checkIfLogin()
  },

  onShow(){
    checkIfLogin()
    this.checkDefaultData()
  },

  onHide(){
    
  },

  checkDefaultData(){
    const globalInfo = app.globalData.globalInfo
    if(!globalInfo.vehicleId){
      this.setData({
        vehicleInfo:{}
      })
    }
    if(!globalInfo.appointPackageId){
      this.setData({
        projectInfo:{}
      })
    }
    let appointTimeInfo = ""
    if(!globalInfo.appointDate){
      this.setData({
        appointmentTimeInfo:{}
      })
    }else{
       appointTimeInfo = globalInfo.appointDate + (globalInfo.appointTime ? (globalInfo.appointTime == "am" ? ' 上午': ' 下午'): '')
    }

    this.setData({
      selectedInfo: globalInfo,
      appointTimeInfo
    })
  },

  navToVehicle(){
    let url = '/pages/select-vehicle/select-vehicle'
    let selectedInfo = this.data.selectedInfo
    if(selectedInfo.vehicleId){
      url = `${url}?id=${selectedInfo.vehicleId}&name=${selectedInfo.plateNo}&vehicleType=${selectedInfo.vehicleType}`
    }
    const _this = this
    wx.navigateTo({
      url,
      events:{
        infoChange(data){
          _this.setData({
            vehicleInfo: data,
          })
        }
      }
    })
  },

  navToProject(){
    let url = '/pages/select-package/select-package'
    let selectedInfo = this.data.selectedInfo
    if(selectedInfo.appointPackageId){
      url = `${url}?id=${selectedInfo.appointPackageId}`
    }
    const _this = this
    wx.navigateTo({
      url,
      events:{
        infoChange(data){
          _this.setData({
            projectInfo: data,
          })
        }
      }
    })
  },

  navToTime(){
    let url = '/pages/select-time/select-time'
    let selectedInfo = this.data.selectedInfo
    if(selectedInfo.appointDate){
      url = `${url}?selectedDate=${selectedInfo.appointDate}&selectedTime=${selectedInfo.appointTime}`
    }
    const _this = this
    wx.navigateTo({
      url,
      events:{
        infoChange(data){
          let appointTimeInfo = data.selectedDate + (data.selectedTime ? (data.selectedTime == "am" ? ' 上午': ' 下午'): '')
          _this.setData({
            appointmentTimeInfo: data,
            appointTimeInfo
          })
        }
      }
    })
  },

  navToPoint(){
    wx.switchTab({
      url: '/pages/inspection-point/inspection-point',
    })
  },

  cancelAppoint(){
    app.globalData.globalInfo = {}
    this.checkDefaultData()
    wx.switchTab({
      url: '/pages/home-page/home-page',
    })
  },

  async submitInfo(){
    const globalInfo = app.globalData.globalInfo
    console.log("globalInfo>>>>", globalInfo)
    if(!globalInfo.vehicleId){
      this.setData({
        showDialogAlert: true,
        dialogMsg:"请先选择送检车辆!",
      })  
      return
    }
    if(!globalInfo.appointPackageId){
      this.setData({
        showDialogAlert: true,
        dialogMsg:"请先选择送检项目!",
      }) 
      return
    }
    if(!globalInfo.appointDate){
      this.setData({
        showDialogAlert: true,
        dialogMsg:"请先选择预约时间!",
      })
      return
    }
    if(!globalInfo.id){
      let addOrderRes = await addOrder(globalInfo)
      if(addOrderRes.code === 200){
        let newData = addOrderRes.data || {}
        let newId  = newData.id || ""
        app.globalData.globalInfo = {}
        this.checkDefaultData()
        wx.redirectTo({
          url: `/pages/appointment-success/appointment-success?newId=${newId}`,
        })
      }
    }else{
      let editOrderRes = await editOrder(globalInfo)
      if(editOrderRes.code === 200){
        let newId  = globalInfo.id || ""
        app.globalData.globalInfo = {}
        this.checkDefaultData()
        wx.redirectTo({
          url: `/pages/appointment-success/appointment-success?newId=${newId}`,
        })
      }
    }


    
  },

  onChange(){}
})